/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author melchiorin
 */
class Rule {
    private Map<String, String> premises = new HashMap<String, String>()
    private Map<String, String> conclusions = new HashMap<String, String>()
    private def id
	
	public void addPremise(key, value)
	{
		this.premises.put (key, value)
	}
	
	public void addConclusion(key, value)
	{
		this.conclusions.put (key, value)
	}
	
	public void addId(id)
	{
		this.id = id
	}
	
	public Map<String, String> getPremises()
	{
		return premises
	}
	
	public Map<String, String> getConclusions()
	{
		return conclusions
	}
	
	public boolean samePremises(Rule r)
	{
		def rPremises = r.getPremises()
		
		def samePremise = true
		this.premises.each
		{
			key, value ->
			def premiseFound = false
			rPremises.each
			{
				key2, value2 ->
				if (key2==key && value2==value)
				{
					premiseFound = true
				}
			}
			samePremise = samePremise && premiseFound
		}
		return samePremise
	}
	
	public boolean sameConclusions(Rule r)
	{
		def rConclusions = r.getConclusions()
		
		def sameConclusion = true
		this.conclusions.each
		{
			key, value ->
			def conclusionFound = false
			rConclusions.each
			{
				key2, value2 ->
				if (key2==key && value2==value)
				{
					conclusionFound = true
				}
			}
			sameConclusion = sameConclusion && conclusionFound
		}
		return sameConclusion
	}

    public String toString(){
        def string = ""
            
        string += "premises:" + getPremisesString()
        string += "; conclusions:" + getConclusionsString()
            
        return string
    }

    public String getConclusionsString(){
        def result = "";
        conclusions.each(){
            key, value -> result += key + "=" + value + ","
        }
        return result;
    }

    public String getPremisesString(){
        def result = "";
        premises.each(){
            key, value -> result += key + "=" + value + ","
        }
        return result;
    }

    public boolean equals(obj){
        boolean result = false
        if(obj instanceof Rule)
		{
			obj = (Rule) obj
			def objPremises = obj.getPremises()
			def objConclusions = obj.getConclusions()
			
			if (objPremises.size() != this.premises.size() ||
				objConclusions.size() != this.conclusions.size())
			{
				return false
			}
		
			if (this.sameConclusions(obj) && this.samePremises(obj))
				result = true
		}

        return result
    }
}

